package com.manong.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.manong.aspect.AutoFill;
import com.manong.entity.Leave;
import com.manong.entity.Shop;
import com.manong.service.LeaveService;
import com.manong.utils.JwtUtils;
import com.manong.utils.Result;
import com.manong.vo.LeaveVo;
import com.manong.vo.query.ShopVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

@RestController
@RequestMapping("/api/leave")
public class LeaveController {
    @Resource
    private HttpServletRequest request;
    @Resource
    private JwtUtils jwtUtils;
    @Resource
    private LeaveService leaveService;

    @GetMapping("/list")
    public Result list(LeaveVo leaveVo){
        IPage<Leave> page = new Page<Leave>();
        page.setCurrent(leaveVo.getPageNo());
        page.setSize(leaveVo.getPageSize());
        leaveVo.setUsername(jwtUtils.getUsernameFromToken(request.getHeader("token")));
        leaveService.findLeaveListByUsername(page,leaveVo);
        return Result.ok(page);
    }

    @AutoFill(menu = "我的假条", type = "新建")
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:leave:add')")
    public Result add(@RequestBody Leave leave) throws Exception {
        leave.setCreateTime(new Date());
        leave.setLeaveTime(new Date());
        leave.setUsername(jwtUtils.getUsernameFromToken(request.getHeader("token")));

        if(leaveService.save(leave)){
            return Result.ok().message("添加成功");
        }else{
            return Result.error().message("添加失败");
        }
    }

    @AutoFill(menu = "我的假条", type = "修改")
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('sys:leave:edit')")
    public Result update(@RequestBody Leave leave){
        //调用修改方法
        if(leaveService.updateById(leave)){
            return Result.ok().message("修改成功");
        }
        //返回数据
        return Result.error().message("修改失败");
    }

    @AutoFill(menu = "我的假条", type = "删除")
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:shop:delete')")
    public Result delete(@PathVariable Long id){
        if(leaveService.deleteById(id)){
            return Result.ok().message("删除成功");
        }else{
            return Result.error().message("删除失败");
        }
    }


    /**
     * 查询所有假条
     * @param leaveVo
     * @return
     */
    @GetMapping("/listAll")
    public Result listAll(LeaveVo leaveVo){
        IPage<Leave> page = new Page<Leave>();
        page.setCurrent(leaveVo.getPageNo());
        page.setSize(leaveVo.getPageSize());
        leaveService.findLeaveListAllByUsername(page,leaveVo);
        return Result.ok(page);
    }

    @AutoFill(menu = "我的假条", type = "审核")
    @PutMapping("/updatePass")
    @PreAuthorize("hasAuthority('sys:leave:audit')")
    public Result updatePass(@RequestBody Leave leave){
        leave.setCheckPass(jwtUtils.getUsernameFromToken(request.getHeader("token")));
        //调用修改方法
        if(leaveService.updateById(leave)){
            return Result.ok().message("成功");
        }
        //返回数据
        return Result.error().message("失败");
    }
}
